package userModule.controller;

import userModule.service.UserService;
import vo.Library_User;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "LoginServlet", value = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        UserService us = new UserService();

        HttpSession session = request.getSession();

        Library_User user = null;

        request.setCharacterEncoding("utf-8");

        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //从Session中获取登录信息
        user = (Library_User) session.getAttribute("user");

        if(null==user){
            user = us.login(username,password);
        }

        //如果返回的用户不为空，表示登录成功
        if(null!=user){
            request.setAttribute("user",user);
            request.getRequestDispatcher("/jsp/modifyUser.jsp").forward(request,response);
        }else{
            //如果返回的用户为空，表示登录失败
            request.setAttribute("backInfo","用户名或密码错误");
            request.getRequestDispatcher("/jsp/login.jsp").forward(request,response);
        }


    }
}
